package com.tian.simulate;

import com.tian.service.ChargeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-12-13 10:21
 * {@code @version:} 1.0
 */
@Component
@Scope("prototype")
@Slf4j
public class ChargingDataReportTask implements Runnable {

    @Resource
    private ChargeRecordService chargeRecordService;

    /**
     * todo 模拟充电枪数据
     * 这里是模拟数据，实际上应该从redis中获取数据
     * 这里只是为了演示，所以直接用一个map来模拟数据
     */
    private static final Map<String, Integer> chargeStationGuns = new ConcurrentHashMap<>();

    static {
        for (int i = 1; i <= 5; i++) {
            chargeStationGuns.put("GUN-" + i, i);
        }
    }

    private String gunNo;

    public void setGunNo(String gunNo) {
        this.gunNo = gunNo;
    }

    @Override
    public void run() {
        log.info("持续充电,数据上报：{}", gunNo);
        try {
            chargeRecordService.chargingDataReport(chargeStationGuns.get(gunNo));
        } catch (Exception e) {
            log.error("充电数据上报失败：{}", e.getMessage());
        }
    }
}
